package com.sitia.jd.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.io.unit.DataUnit;
import com.alibaba.fastjson2.JSON;
import org.jsoup.helper.DataUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.sitia.common.annotation.Log;
import com.sitia.common.core.controller.BaseController;
import com.sitia.common.core.domain.AjaxResult;
import com.sitia.common.enums.BusinessType;
import com.sitia.jd.domain.MxRebateOrder;
import com.sitia.jd.service.IMxRebateOrderService;
import com.sitia.common.utils.poi.ExcelUtil;
import com.sitia.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 返利订单Controller
 * 
 * @author sitia
 * @date 2024-03-08
 */
@RestController
@RequestMapping("/jd/rebate/order")
public class MxRebateOrderController extends BaseController
{
    @Autowired
    private IMxRebateOrderService mxRebateOrderService;

    /**
     * 查询返利订单列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MxRebateOrder mxRebateOrder)
    {
        startPage();
        List<MxRebateOrder> list = mxRebateOrderService.selectMxRebateOrderList(mxRebateOrder);
        return getDataTable(list);
    }
    /**
     * 查询返利订单列表
     */
    @GetMapping("/pageList")
    public TableDataInfo pageList(MxRebateOrder mxRebateOrder)
    {
        startPage();
        List<MxRebateOrder> list = mxRebateOrderService.pageList(mxRebateOrder);
        return getDataTable(list);
    }

    /**
     * 导出返利订单列表
     */
    @Log(title = "返利订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MxRebateOrder mxRebateOrder)
    {
        List<MxRebateOrder> list = mxRebateOrderService.selectMxRebateOrderList(mxRebateOrder);
        ExcelUtil<MxRebateOrder> util = new ExcelUtil<MxRebateOrder>(MxRebateOrder.class);
        util.exportExcel(response, list, "返利订单数据");
    }

    /**
     * 获取返利订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('jd:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(mxRebateOrderService.selectMxRebateOrderById(id));
    }

    /**
     * 新增返利订单
     */
    @Log(title = "返利订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MxRebateOrder mxRebateOrder)
    {
        return toAjax(mxRebateOrderService.insertMxRebateOrder(mxRebateOrder));
    }

    /**
     * 修改返利订单
     */
    @Log(title = "返利订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MxRebateOrder mxRebateOrder)
    {
        return toAjax(mxRebateOrderService.updateMxRebateOrder(mxRebateOrder));
    }

    /**
     * 删除返利订单
     */
    @Log(title = "返利订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(mxRebateOrderService.deleteMxRebateOrderByIds(ids));
    }

    /**
     * 导入订单id和数组Excel文件，生成订单
     *
     * @param fileList 文件
     * @param
     * @return
     */
    @PostMapping("/importIdAndOrderExcel")
    public AjaxResult importIdAndOrderExcel(@RequestParam("fileList") List<MultipartFile> fileList) {

        return AjaxResult.success(mxRebateOrderService.importIdAndOrderExcel(fileList, null));
    }

    /**
     * 京粉数据推送

     * @return
     */
    @PostMapping("/importJinFenData")
    public AjaxResult importJinFenData(List<MxRebateOrder> list) {
        return AjaxResult.success();
    }

    /**
     * 导入京粉CSV
     *
     * @param fileList 文件
     * @param
     * @return
     */
    @PostMapping("/importJinFenCsv")
    public AjaxResult importJinFenCsv(@RequestParam("fileList") MultipartFile fileList,@RequestParam("data") String data) {

        MxRebateOrder mxRebateOrder = JSON.parseObject(data, MxRebateOrder.class);
        return AjaxResult.success(mxRebateOrderService.importJinFenCsv(fileList, mxRebateOrder));
    }

    /**
     * 显示返利饼图统计图
     *
     * @param
     * @param
     * @return
     */
    @GetMapping("/queryRebatePie")
    public AjaxResult queryRebatePie( MxRebateOrder mxRebateOrder) {

        return AjaxResult.success(mxRebateOrderService.queryRebatePie(mxRebateOrder));
    }

    /**
     * 返利统计
     * @param mxRebateOrder
     * @return
     */
    @GetMapping("/rebateStatistics")
    public AjaxResult rebateStatistics( MxRebateOrder mxRebateOrder) {

        return AjaxResult.success(mxRebateOrderService.rebateStatistics(mxRebateOrder));
    }
    /**
     * 返利统计
     * @param mxRebateOrder
     * @return
     */
    @GetMapping("/rebateChart")
    public AjaxResult rebateChart( MxRebateOrder mxRebateOrder) {

        return AjaxResult.success(mxRebateOrderService.rebateChart(mxRebateOrder));
    }
}
